package com.jeomo.mem.provider.web;

import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.mem.provider.request.*;
import com.jeomo.mem.provider.vo.MemberPointsSuppleVo;
import com.jeomo.mem.starter.dto.MemberSuppleDto;
import com.jeomo.mem.starter.entity.MemberPointsSuppleDetail;
import com.jeomo.mem.starter.entity.MemberPointsSupplement;
import com.jeomo.mem.starter.service.IMemPointsSuppleService;
import com.jeomo.mem.starter.service.IMemPointsSupplementService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 会员积分补录
 *
 * @author:Jackpot
 * @create 2020-10-14 16:24
 * @description:
 */
@RestController
@RequestMapping("/member/supple")
public class MemberPointsSuppleController {

    @Autowired
    private IMemPointsSuppleService memPointsSuppleService;

    @Resource
    private IMemPointsSupplementService memPointsSupplementService;

    @ApiOperation("会员积分补录接口")
    @PostMapping("/points")
    public CommonResult saveRule(@RequestBody @Validated MemPointsSuppleReq memPointsSuppleReq){

        MemberPointsSupplement memberPointsSupple = BeanCopyUtil.copyProperties(memPointsSuppleReq, MemberPointsSupplement.class);
        int resultCode = memPointsSuppleService.insertPointSupple(memberPointsSupple);
        if (resultCode >0){
            return CommonResult.success("保存成功");
        }else if (resultCode == -1){
            return CommonResult.failure("数据已发生变化,请刷新后重试！");
        }else if (resultCode == -2){
            return CommonResult.failure("数据已被删除，操作失败！");
        }else if (resultCode == -3){
            return CommonResult.failure("配置项不能重复！");
        }else{
            return CommonResult.failure("保存失败");
        }
    }


    @ApiOperation("分页获取会员补录信息")
    @PostMapping("/points/page")
    public CommonResult<PageResponseVo<MemberPointsSuppleVo>> getPointRulesByPage(@RequestBody PageRequest pageRequest){
        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        PageResponseDto pageResponseDto = memPointsSupplementService.pageSearch(dto);
        PageResponseVo<MemberPointsSuppleVo> memPointsRulePageResponseVo =
                BeanCopyUtil.copyPageProperties(pageResponseDto, MemberPointsSuppleVo.class);
        return CommonResult.success(memPointsRulePageResponseVo);
    }

    @ApiOperation("获取一条积分补录的详情信息")
    @GetMapping("/details/{code}")
    public CommonResult getPointRuleById(@PathVariable String code){
        MemberSuppleDto memberPointSupple = memPointsSupplementService.getMemberPointSupple(code);
        return CommonResult.success(memberPointSupple);
    }


    @ApiOperation("审批")
    @PostMapping("/workflow")
    public CommonResult updateRuleStatus(@RequestBody ApprovalWorkFlow approvalWorkFlow){

        String code = approvalWorkFlow.getCode();
        String auditor = approvalWorkFlow.getAuditor();
        int status = approvalWorkFlow.getStatus();
        int i = memPointsSupplementService.updateWorkflowStatus(code, auditor, status);
        return i>0?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

    @ApiOperation("查询商品消费信息")
    @PostMapping("/consumer")
    public CommonResult queryGoodsConsumerInfo(@RequestBody GoodsConsumerInfo goodsConsumerInfo) {

        Date date=goodsConsumerInfo.getPayTime();
        String levelCode=goodsConsumerInfo.getLevelCode();
        String merchant=goodsConsumerInfo.getMerchant();
        double amount=goodsConsumerInfo.getAmount();
        List<MemberPointsSuppleDetail> memberPointsSuppleDetails = memPointsSupplementService.queryGoodsConsumerInfo(date, levelCode, merchant, amount);
        return CommonResult.success(memberPointsSuppleDetails);
    }


}
